﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.SessionState;
using System.Collections.Specialized;
using NTTHAO.Common.BL;

namespace NTTHAO.CommonWeb
{
    /// <summary>
    /// Summary description for LanguageUtility
    /// </summary>
    public class LanguageUtility
    {
        private LanguageUtility() { }

        protected static void LoadLanguage(HttpSessionState Session, int langID)
        {
            LanguageText languageText = new LanguageText();
            System.Data.DataSet languageValues = languageText.GetLanguageText(langID);

            NameValueCollection coll = new NameValueCollection();
            if (languageValues != null && languageValues.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < languageValues.Tables[0].Rows.Count; i++)
                {
                    coll.Add(languageValues.Tables[0].Rows[i]["name"].ToString(), languageValues.Tables[0].Rows[i]["text"].ToString());
                }
            }

            Session.Remove(Constant.LANGUAGE_SESSION);
            Session.Add(Constant.LANGUAGE_SESSION, coll);
        }

        public static void SetLanguage(int langID, HttpRequest request, HttpResponse response, HttpSessionState session)
        {
            try
            {
                if (CookieUtility.SupportCookie(request))
                {
                    CookieUtility.SetCookie(NTTHAO.CommonWeb.Constant.LANGUAGE_ID_SESSION, langID.ToString(), DateTime.Now.AddDays(90), request, response);
                }
                else
                {
                    session.Remove(NTTHAO.CommonWeb.Constant.LANGUAGE_ID_SESSION);
                    session.Add(NTTHAO.CommonWeb.Constant.LANGUAGE_ID_SESSION, langID);
                }
                LanguageUtility.LoadLanguage(session, langID);
            }
            catch (Exception) { }
        }

        public static int GetLanguageID(HttpRequest request, HttpSessionState session)
        {
            try
            {
                if (CookieUtility.SupportCookie(request))
                {
                    return Int32.Parse(CookieUtility.GetCookieValue(NTTHAO.CommonWeb.Constant.LANGUAGE_ID_SESSION, request));
                }
                else if (NTTHAO.CommonWeb.WebGlobal.GetSite() != null)
                {
                    return NTTHAO.CommonWeb.WebGlobal.GetSite().DefaultLanguage;
                }
                else
                {
                    return (int)session[NTTHAO.CommonWeb.Constant.LANGUAGE_ID_SESSION];
                }
            }
            catch (Exception)
            {
            }
            return NTTHAO.CommonWeb.Constant.DEFAULT_LANGUAGE;
        }

        public static int GetLanguageID()
        {
            return LanguageUtility.GetLanguageID(HttpContext.Current.Request, HttpContext.Current.Session);
        }

        public static void CheckLanguage(HttpRequest request, HttpResponse response, HttpSessionState session)
        {
            if (session[Constant.LANGUAGE_SESSION] == null)
            {
                LanguageUtility.ReloadLanguage(request, response, session);
            }
        }

        public static void ReloadLanguage(HttpRequest request, HttpResponse response, HttpSessionState session)
        {
            LanguageUtility.SetLanguage(LanguageUtility.GetLanguageID(request, session), request, response, session);
        }

        public static string GetLanguageText(string name, HttpSessionState Session)
        {
            try
            {
                if (Session[Constant.LANGUAGE_SESSION] != null)
                {
                    return ((NameValueCollection)Session[Constant.LANGUAGE_SESSION])[name].Replace("'", "\\'");
                }
            }
            catch (Exception)
            {
            }
            return "";
        }

        public static string GetTrueLanguageText(string name, HttpSessionState Session)
        {
            try
            {
                if (Session[Constant.LANGUAGE_SESSION] != null)
                {
                    return ((NameValueCollection)Session[Constant.LANGUAGE_SESSION])[name];
                }
            }
            catch (Exception)
            {
            }
            return "";
        }

        public static string GetLanguageName(int langID, HttpSessionState Session)
        {
            try
            {
                Language lang = new Language(langID);
                if (lang.ID > 0)
                {
                    return lang.Name;
                }
            }
            catch (Exception)
            {
            }
            return String.Empty;
        }

        public static string GetLanguageText(string name)
        {
            try
            {
                if (HttpContext.Current.Session[Constant.LANGUAGE_SESSION] != null)
                {
                    return ((NameValueCollection)HttpContext.Current.Session[Constant.LANGUAGE_SESSION])[name].Replace("'", "\\'");
                }
            }
            catch (Exception)
            {
            }
            return "";
        }

        public static string ReadDay(DateTime day, int langID, bool fullText)
        {
            string result = "";
            if (langID == 1)
            {
                if (fullText)
                {
                    result = (day.DayOfWeek == DayOfWeek.Sunday ? "Chủ Nhật" : ("Thứ " + ((int)day.DayOfWeek + 1).ToString()))
                        + ", ngày " + day.Day.ToString() + " tháng " + day.Month.ToString() + " năm " + day.Year.ToString();
                }
                else
                {
                    result = (day.DayOfWeek == DayOfWeek.Sunday ? "Chủ Nhật" : ("Thứ " + ((int)day.DayOfWeek + 1).ToString()))
                         + ", ngày " + day.ToString("dd/MM/yyyy");
                }
            }
            else
            {
                result = day.ToLongDateString();
            }
            return result;
        }

        public static string ReadDayWithMonth(DateTime day, int langID, bool dayOfWeek, bool dayYearPrefix)
        {
            string result = "";
            if (langID == 1)
            {
                if (dayOfWeek)
                {
                    result = (day.DayOfWeek == DayOfWeek.Sunday ? "Chủ Nhật" : ("Thứ " + ((int)day.DayOfWeek + 1).ToString())) + ", ";
                }

                string[] monthOfYear = new string[] { "Tháng Giêng", "Tháng Hai", "Tháng Ba", "Tháng Tư", "Tháng Năm", "Tháng Sáu", "Tháng Bảy", "Tháng Tám", "Tháng Chín", "Tháng Mười", "Tháng Mười Một", "Tháng Mười Hai" };
                
                result += (dayYearPrefix ? "ngày " : "") + day.Day.ToString() + " " 
                            + monthOfYear[day.Month - 1] 
                            + (dayYearPrefix ? " năm " : " ") + day.Year.ToString();
                
            }
            else
            {
                result = day.ToLongDateString();
            }
            return result;
        }
    }
}